package risk.rest.rcv.login.impl

import edu.fudan.mylang.pf.BaseService
import edu.fudan.langlab.domain.security.ISystemUserService
import edu.fudan.langlab.domain.security.User
import com.uniquesoft.gwt.server.service.common.HttpRequestThreadLocal
import javax.servlet.http.HttpServletRequest
import org.springframework.stereotype.Service

@Service("risk.rest.rcv.login.impl.SystemUserServiceImpl")
class SystemUserServiceImpl extends BaseService implements ISystemUserService{
	val static USER_ID = "__userid__"
	
	override User getCurrentUser() {
		val HttpServletRequest req = HttpRequestThreadLocal.get();
		if(req==null) return null;
		val userId = req.getSession().getAttribute(USER_ID) as Long
		if(userId != null){
			return this.objectFactory.<User> get(User, userId);
		}
		return null
	}
	
	override getCurrentUserAsParty() {
		//throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}
	

	override logout() {
		HttpRequestThreadLocal.get().getSession().removeAttribute(USER_ID)
	}
	
	override void setCurrentUser(User user) {
		HttpRequestThreadLocal.get().getSession().setAttribute(USER_ID, user.id)
	}
	
}